--[[
@classmod Tile
]]

local lume = require (LIB_PATH.."lume")

return pl.class {
	_name = "Tile", 
	_init = function (self, x, y, s)
		self.x = x or 0
		self.y = y or 0
		self.s = s or Sled.tile_size
		return self
	end,
	
	is_in_map = function (self)
		local w, h = Sled.current_layer.w, Sled.current_layer.h
		local txmax = w-self.s
		local tymax = h-self.s
		if self.x < 0 or self.x > txmax or self.y < 0 or self.y > tymax then 
			return 
		end
		return true
	end,
	
	get_position = function (self)
		return self.x, self.y
	end,
	
	is_equal = function (self, tile)
		if not tile or not tile.is_a then return false end
		if tile.x == self.x and tile.y == self.y then
			return true
		end
		return false
	end,
	
	select = function (self)
		if self:is_equal(Sled.current_tile) then return end
		if not Sled.current_layer then return end
		local w, h = Sled.current_layer.w, Sled.current_layer.h
		local txmax = w-self.s
		local tymax = h-self.s
		if self.x < 0 or self.x > txmax or self.y < 0 or self.y > tymax then return end
		self.y = lume.clamp(self.y, 0, h-self.s)
		Sled.grid:highlight(self.x, self.y, {.7, .3, .5, 1})
		self:copy()
		Sled.current_tile = self
		Sled.current_marker = Sled.current_map:marker_get(self.x, self.y)
		return self
	end,
	
	clear = function (self, layer)
		local layer = Sled.current_layer
		layer.cvs:renderTo(function ()
			local x, y = self.x, self.y
			_lg.setScissor( x, y, self.s, self.s)
			_lg.clear()
			_lg.setScissor()
		end)
	end,
	
	focus = function (self)
		Sled.current_layer:focus_on(self.x, self.y, self.s, self.s)
		self:copy ()
	end,
	
	copy = function (self)
		Sled.tile_buffer = self:get_img_data()
		Sled.gui.tile_display_widget:redraw()
	end,
	
	paste = function (self)
		local layer = Sled.current_layer
		local data = Sled.tile_buffer
		if not data then return end
		self:clear()
		layer.cvs:renderTo(function ()
			local x, y = self.x, self.y
			local img = _lg.newImage(data)
			_lg.draw(img, x, y)
		end)
	end,
	
	paste_with_marker = function (self, marker)
		self:paste()
		self:add_marker(marker)
	end,
	
	add_marker = function (self, marker)
		local name, c = marker:get_name()
		local count = tonumber(c) or lume.count(Sled.current_map.markers,
			function (m)
				return m:get_name() == name
			end)
		Sled.current_map:marker_add(name.."("..(count+1)..")", self.x, self.y)
	end,
	
	remove_marker = function (self)
		local m = Sled.current_marker
		if not m then return end
		lume.remove(Sled.current_map.markers, m)
		Sled.current_marker = nil
	end,
	
	flip = function (self, dir)
		local sx, sy = 1, 1
		local nx, ny = self.x, self.y
		local s = self.s
		if dir == "h" then
			sx = -1
			nx = nx + s
		elseif dir == "v" then
			sy = -1
			ny = ny + s
		end
		self:clear()
		local layer = Sled.current_layer
		local data = Sled.tile_buffer
		layer.cvs:renderTo(function ()
			local x, y = self.x, self.y
			local img = _lg.newImage(data)
			_lg.draw(img, nx, ny, nil, sx, sy)
		end)
		Sled.tile_buffer = self:get_img_data ()
	end,
	
	get_img_data = function (self)
		if not Sled.current_layer then return end
		return Sled.current_layer.cvs:newImageData(nil, nil, self.x, self.y, self.s, self.s)
	end,
	
	set_img_data = function (self, img_data)
		if not Sled.current_layer then return end
		local img_data = img_data or TileBuff
		self:paste(img_data)
	end,
	
	export = function (self, filename)
		local data = self:get_img_data ()
		data:encode("png", "tiles/" .. filename..".png")
	end,
	
	save = function (self)
	
	end,
}
